if SERVER then
	AddCSLuaFile( "shared.lua" )
end

if CLIENT then
	SWEP.Slot				= 3
	SWEP.SlotPos			= 1
	SWEP.DrawCrosshair      = false
	SWEP.IconLetter = ""
	SWEP.DrawAmmo = false
end

SWEP.PrintName			= "Arrest stick"
SWEP.Author				= "Arran"
SWEP.Instructions		= "Left click near a player to arrest them."
	
SWEP.Spawnable			= false
SWEP.AdminSpawnable		= true

SWEP.ViewModel			= "models/weapons/v_stunstick.mdl"
SWEP.WorldModel			= "models/weapons/w_stunbaton.mdl"

SWEP.Primary.ClipSize		= -1
SWEP.Primary.DefaultClip	= -1
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= ""
SWEP.Primary.Delay			= 1

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo			= ""

function SWEP:Initialize()
	self:SetWeaponHoldType( "melee" )
	
	self.Hit = { 
	Sound( "weapons/stunstick/stunstick_impact1.wav" ),
	Sound( "weapons/stunstick/stunstick_impact2.wav" ) 
	}
        
	self.FleshHit = {
	Sound( "weapons/stunstick/stunstick_fleshhit1.wav" ),
	Sound( "weapons/stunstick/stunstick_fleshhit2.wav") 
	}
	
	self.JailPos = {
	Vector( 500, 750, -80 ),
	Vector( 400, 700, -80 )
	}
	
end

function SWEP:PrimaryAttack()

if ( CurTime() < self.Primary.Delay	) then return end
self.Primary.Delay = CurTime() + 1

self.Owner:SetAnimation( PLAYER_ATTACK1 )
self.Weapon:SendWeaponAnim( ACT_VM_MISSCENTER )

if SERVER then

local trace = self.Owner:GetEyeTrace()

	if (trace.Entity:IsPlayer()) and ( self.Owner:EyePos():Distance( trace.Entity:GetPos() ) < 100 ) then
		self.Owner:EmitSound( self.FleshHit[math.random(1, #self.FleshHit)] )
		
		if (trace.Entity:IsPlayer()) and (!trace.Entity.IsJailed) and ( self.Owner:EyePos():Distance( trace.Entity:GetPos() ) < 100 ) then
			trace.Entity:SetPos( self.JailPos[math.random(1, #self.JailPos)] )
			trace.Entity.IsJailed = true
			trace.Entity:StripWeapons()
			timer.Create(trace.Entity:EntIndex().."_Arrest", 120, 1, function() ReleaseMe(trace.Entity) end)
		else
			if (trace.Entity:IsPlayer()) and (trace.Entity.IsJailed) and ( self.Owner:EyePos():Distance( trace.Entity:GetPos() ) < 100 ) then
				self.Owner:SendLua("GAMEMODE:AddNotify(\"This person is already arrested!\", NOTIFY_GENERIC, 5)")
			end
		end
	
	else
		self.Owner:EmitSound( self.Hit[math.random(1, #self.Hit)] )
		end
	end
end

function JailTimer()
	if CLIENT then
		if LocalPlayer.IsJailed then 
			
		
		end
	end
end

function ReleaseMe(ply)
ply:KillSilent()
ply:Spawn()
ply.IsJailed = false
end

function ReleaseOnDisconnect(ply)
ply.IsJailed = false
end

hook.Add("PlayerDisconnected", "kthardisconnectk", ReleaseOnDisconnect)
